﻿
using IHISCETeim.BLL;
using IHISCETeim.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using IHISCETeim.Library;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class AgentQltyEdit : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.修改 };
            }
        }
        protected override string DWZRef
        {
            get { return "AgentQltyList"; }
        }

        protected int QltyId
        {
            get
            {

                return GetRequestValue<int>("QltyId", 0);
            }
        }
        /// <summary>
        /// 是否来自生成附属品情,0为否
        /// </summary>
        protected int isAffiliated
        {
            get
            {

                return GetRequestValue<int>("isAffiliated", 0);
            }
        }

        private AgentQltyInfo _objFx = null;

        public AgentQltyInfo obj
        {
            get
            {
                if (_objFx == null)
                    _objFx = AgentQltyController.GetByQltyId(QltyId);
                return _objFx;
            }
        }
        protected string GetSvrInfo()
        {
            OUserInfo svrUser = OUserController.GetById(obj.SrvActUserId);
            return string.Format("{0} {1}", svrUser.RealName, svrUser.Mobile);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "edit")
            {
                add();
            }
            //JsonConvert.SerializeObject(obj)
            LoadInfo();
        }

        private void LoadInfo()
        {
            try
            {
                List<AgentQltyImgInfo> listImg = AgentQltyController.GetAllImgByQltyId_0(QltyId);
                List<AgentQltyImgInfo> listImgT = new List<AgentQltyImgInfo>();
                for (int i = 0; i < 6; i++)
                {
                    listImgT.Add(listImg[i]);
                }
                this.rpImgs.DataSource = listImgT;
                this.rpImgs.DataBind();              
            }
            catch
            {

            }

            List<AgentQltyPartsInfo> listParts = AgentQltyController.GetAllPartsByQltyId(QltyId);
            if (listParts.Count < 20)
            {
                int addNum = 20 - listParts.Count;
                while (addNum > 0)
                {
                    listParts.Add(new AgentQltyPartsInfo() { ID = addNum, PartsDemand = "" });
                    addNum = addNum - 1;
                }
            }

            this.rpParts.DataSource = listParts;
            this.rpParts.DataBind();

            this.rpReject.DataSource = AgentQltyController.AgentQltyReject_GetAllByQltyId(QltyId);
            this.rpReject.DataBind();
        }

       
        protected string GetUploadFile(int index)
        {
            try
            {
                List<AgentQltyImgInfo> list = AgentQltyController.GetAllImgByQltyId_0(QltyId);
                return list[index].IMGRelatively;
            }
            catch { 
                return "";
            }
        }

        private void add()
        {
            AgentQltyInfo obj2 = Request.SerializableObject<AgentQltyInfo>();
            obj2.IsVisit = false;
            obj2.SvrResult = obj.SvrResult;
            obj2.SvrScheme = obj.SvrScheme;
            obj2.PartsSendDate = obj.PartsSendDate;
            obj2.PartsSendType = obj.PartsSendType;
            obj2.IsCancelReason = false;
            obj2.CancelReason = obj.CancelReason;
            obj2.MainFaultID = obj.MainFaultID;
            obj2.UserId = this.User.UserId;
            obj2.AgentName = this.User.Department;
            obj2.SrvActUserId = this.User.SrvActUserId;
            obj2.SrvActRealName = OUserController.GetById(User.SrvActUserId).RealName;
            //obj.SrvMangerUserId = AgentConfigController.SvrManagerUserId;
            obj2.ClaimState = ClaimState.担当确认中.ToString();
            obj2.RejectReason =isAffiliated==0?obj.RejectReason: "";
            obj2.AgentSvrTel = "";
            obj2.AgentSvrUserId = 0;
            obj2.IsMTTR = obj.IsMTTR;
            obj2.IsYJGL = obj.IsYJGL;
            char[] caSplit = new char[] { ',' };
            //加载图片信息
            AgentQltyImgInfo imgObj = new AgentQltyImgInfo();
            List<AgentQltyImgInfo> imgList = new List<AgentQltyImgInfo>();
            string[] imgRelativelyS = GetRequestValue<string>("imgRelatively", "").Split(caSplit);
            string[] ImgTitleS = GetRequestValue<string>("ImgTitle", "").Split(caSplit);
            for (int index = 0; index < imgRelativelyS.Count(); index++)
            {

                imgObj = new AgentQltyImgInfo();
                imgObj.Title = ImgTitleS[index];
                imgObj.IMGRelatively = imgRelativelyS[index];
                imgObj.FileIndex = index + 1;
                imgObj.FromType = 0;
                imgList.Add(imgObj);

            }

            //加载配件信息
            List<AgentQltyPartsInfo> partsList = new List<AgentQltyPartsInfo>();
            string[] ItemCodes = GetRequestValue<string>("ItemCode", "").Split(caSplit);
            string[] Dscriptions = GetRequestValue<string>("Dscription", "").Split(caSplit);
            string[] Quantitys = GetRequestValue<string>("Quantity", "").Split(caSplit);
            string[] PartsDemands = GetRequestValue<string>("PartsDemand", "").Split(caSplit);
            for (int index = 0; index < ItemCodes.Count(); index++)
            {
                if (ItemCodes[index] != "")
                {
                    AgentQltyPartsInfo pobj = new AgentQltyPartsInfo();
                    pobj.ItemCode = ItemCodes[index];
                    pobj.Dscription = Dscriptions[index];
                    pobj.Quantity = Convert.ToDecimal(Quantitys[index]);
                    pobj.PartsDemand = PartsDemands[index]; ;
                    pobj.LastPurPrice = Convert.ToDecimal(SBOController.GetOITMLastPurPrice(pobj.ItemCode));
                    partsList.Add(pobj);
                }
            }
            //这里处理是生成附属品情，还是重新提交
            if (isAffiliated == 0)
            {
                if (AgentQltyController.UpdateCancelQlty(obj2, imgList, partsList))
                {
                    SuccessJson(String.Format("已重新提交<br/>您的品情已提交到服务担当{0}进行处理.", obj.SrvActRealName), "AgentQltyList");
                }
                else
                {
                    ErrorJson("重新提交品情表失败！");
                }
            }
            else
            {
                obj2.ClaimState = IHISCETeim.Web.ClaimState.担当确认中.ToString();
                obj2.ClaimsNumber = AgentQltyController.GetNewClaimsNumber(obj2.ClaimsNumber);
                obj2.ClaimState = IHISCETeim.Web.ClaimState.担当确认中.ToString();
                if (AgentQltyController.Add(obj2, imgList, partsList, true) > 0)
                {
                    SuccessJson(String.Format("已提交附属品情{1}<br/>您的品情已提交到服务担当{0}进行处理.", obj2.SrvActRealName, obj2.ClaimsNumber), "AgentQltyList");
                }
                else {
                    ErrorJson("提交附属品情失败！");
                }
            }

            //  SuccessJson("提交成功<br/>" + XX, "AgentQltyList");
        }
    }

   
}